import { pointsadd, selectall } from "../../../server/api";
import { debounce } from "../../../server/shake";
import { chooseMap } from "../../../server/pulbmethod";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hdaddress: "", //活动地点
    show: false,
    showend: false,
    minDate: new Date().getTime(),
    maxDate: new Date(2019, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    timelength: "8",
    reson1: "",
    contents: "", //选择规则详情
    gzdetail: "",
    starttime: "", //开始时间
    endtime: "", //结束时间
    kshm: 0,
    jshm: 0,
    gzshow: false,
    radio: "",
    obj: {},
    list: [],
    yLongitude: "", //经度
    xDimension: "", //维度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ obj: JSON.parse(options.msg) });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getgz();
  },
  //获取积分规则数据
  async getgz() {
    const res = await selectall();
    if (res.code == 200) {
      this.setData({ list: res.result });
    }
  },
  onClose() {
    this.setData({ show: false });
  },
  onClose2() {
    this.setData({ showend: false });
  },
  select() {
    this.setData({ show: true });
  },
  selectend() {
    this.setData({ showend: true });
  },
  onInputend(event) {
    const difference = this.hoursBetween(
      this.data.starttime,
      this.msToDate(event.detail)
    );
    const sum = Math.ceil(difference / 24) * 8;
    this.setData({
      endtime: this.msToDate(event.detail),
      showend: false,
      timelength: sum,
    });
    this.setData({ jshm: event.detail });
  },
  onInputs(event) {
    this.setData({ jshm: event.detail });
    this.setData({
      starttime: this.msToDate(event.detail),
      kshm: event.detail,
    });
    const difference = this.hoursBetween(
      this.data.starttime,
      this.msToDate(this.data.endtime)
    );
    const sum = Math.ceil(difference / 24) * 8;
    this.setData({ show: false, timelength: sum });
  },
  msToDate(milliseconds) {
    var date = new Date(milliseconds);
    var year = date.getFullYear();
    var month = ("0" + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
    var day = ("0" + date.getDate()).slice(-2);
    var hours = ("0" + date.getHours()).slice(-2);
    var minutes = ("0" + date.getMinutes()).slice(-2);
    return year + "-" + month + "-" + day + " " + hours + ":" + minutes;
  },
  hoursBetween(startDate, endDate) {
    const oneHour = 1000 * 60 * 60; // 1小时的毫秒数
    const startTime = new Date(startDate).getTime(); // 将日期转换为毫秒时间戳
    const endTime = new Date(endDate).getTime(); // 同上
    const difference = endTime - startTime; // 计算时间差（毫秒）
    // 将时间差转换为小时
    const hours = difference / oneHour;
    return hours;
  },
  onChangeval(e) {
    this.setData({ timelength: e.detail });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  //积分规则点击事件
  selectgz() {
    this.setData({ gzshow: true });
  },
  //选择积分规则
  onClick(event) {
    const { name, content, gzname } = event.currentTarget.dataset;
    this.setData({
      radio: name,
      contents: content,
      gzname: gzname,
    });
  },
  cel() {
    this.setData({ gzshow: false, radio: "", contents: "", gzname: "" });
  },
  qued() {
    this.setData({
      gzshow: false,
      reson1: this.data.gzname,
      gzdetail: this.data.contents,
    });
  },
  //发布
  passed: debounce(async function () {
    const {
      hdaddress,
      starttime,
      endtime,
      radio,
      timelength,
      yLongitude,
      xDimension,
    } = this.data;
    if (hdaddress == "") {
      wx.showToast({
        title: "活动地点不能为空",
        icon: "none",
      });
    } else if (starttime == "" || endtime == "") {
      wx.showToast({
        title: "活动时间不能为空",
        icon: "none",
      });
    } else if (starttime > endtime) {
      wx.showToast({
        title: "开始时间不能大于结束时间",
        icon: "none",
      });
    } else if (radio == "") {
      wx.showToast({
        title: "积分规则不能为空",
        icon: "none",
      });
    } else {
      const params = {
        address: hdaddress,
        beginTime: starttime,
        endTime: endtime,
        rulesId: radio,
        duration: timelength <= 0 ? 0 : timelength,
        picturePath: this.data.obj.img,
        activitypicturePath: this.data.obj.img2,
        title: this.data.obj.title,
        remark: this.data.obj.content,
        xdimension: String(xDimension),
        ylongitude: String(yLongitude),
      };
      console.log(params, "ppppp");
      const res = await pointsadd(params);
      if (res.code == 200) {
        wx.showToast({
          title: "发布成功",
          icon: "success",
        });
        setTimeout(() => {
          wx.reLaunch({
            url: "../../../pages/management/management",
          });
        }, 1000);
      }
    }
  }, 3000),
  reject() {
    wx.navigateBack(-1);
  },
  //选择活动地点
  openadd() {
    const that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          hdaddress: res.name,
          xDimension: res.latitude,
          yLongitude: res.longitude,
        });
        // console.log(res.name); // 选择的位置名称
        // console.log(res.address); // 选择的位置地址
        console.log(res.latitude); // 选择的位置纬度
        console.log(res.longitude); // 选择的位置经度
      },
    });
  },
});
